// Package controllers
// @Title  articleTags.go
// @Description  文章标签
// @Author  小斌 (时间 格式是2021/12/02  11:40)
// @Update  小斌 (时间 格式是2021/12/02  11:40)

package controllers

import (
	"beego-admin/models"
	"time"
)

type ArticleTagsController struct {
	BaseController
}

func (this *ArticleTagsController) Index() {
	this.TplName = "admin/article/article_tags_list.tpl"
}

func (this *ArticleTagsController) ArticleTagsAdd() {
	this.TplName = "admin/article/article_tags_add.tpl"
}

func (this *ArticleTagsController) ArticleTagsEdit() {
	id,_ := this.GetInt("id",0)
	articleCatInfo,_ := models.GetArticleTagsInfoById(id)

	this.Data["article_tags_info"] = articleCatInfo
	this.TplName = "admin/article/article_tags_edit.tpl"
}

func (this *ArticleTagsController) GetArticleTagsList(){
	page,_ := this.GetInt("page",1)
	pageSize,_ := this.GetInt("limit",5)
	list, count, _ := models.GetArticleTagsList(pageSize,page)

	type CarouseObj struct{
		Id                 int    `json:"id"`
		TagsName				string `json:"tags_name"`
		Status               int `json:"status"`
		StatusName               string `json:"status_name"`
		UpdateTime                time.Time `json:"update_time"`
		AddTime                time.Time `json:"add_time"`
	}
	var returnData[]CarouseObj
	for _, v := range *list {
		statusName := "启用"
		if v.Status != 0{
			statusName = "停用"
		}

		tmpParam := CarouseObj{v.Id,v.TagsName,v.Status,statusName,v.UpdateTime,v.AddTime}
		returnData = append(returnData, tmpParam)
	}


	this.Data["json"] = this.AjaxLayuiTableReturn(0,"成功",count,returnData)
	this.ServeJSON()
}


func (this *ArticleTagsController) AjaxAddArticleTags() {
	tagsName := this.GetString("tags_name")
	status,_ := this.GetInt("status",0)
	if tagsName == ""{
		this.Data["json"] = this.AjaxReturn(10001,"标签名称不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.AddArticleTags(tagsName,status)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"添加成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"添加失败",nil)
	}

	this.ServeJSON()
}

func (this *ArticleTagsController) AjaxEditArticleTags() {
	articleTagsId,_ := this.GetInt("id",0)
	tagsName := this.GetString("tags_name")
	status,_ := this.GetInt("status",0)
	if tagsName == ""{
		this.Data["json"] = this.AjaxReturn(10001,"目录名称不能为空",nil)
		this.ServeJSON()
		this.StopRun()
	}

	id := models.UpdateArticleTagsById(articleTagsId,tagsName,status)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"修改成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"修改失败",nil)
	}

	this.ServeJSON()
}

// AjaxDeleteArticleTags ajax删除标签
func (this *ArticleTagsController) AjaxDeleteArticleTags() {
	articleTagsId,_ := this.GetInt("id",0)

	adminUserInfo,_ := models.GetArticleTagsInfoById(articleTagsId)
	if adminUserInfo.Id <= 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除的记录不存在",nil)
		this.ServeJSON()
		this.StopRun()
	}
	id := models.DeleteArticleTagsById(articleTagsId)
	if id > 0{
		this.Data["json"] = this.AjaxReturn(10000,"删除成功",nil)
	}else{
		this.Data["json"] = this.AjaxReturn(10001,"删除失败",nil)
	}

	this.ServeJSON()
}